using UnityEngine;

public class DartController : MonoBehaviour
{
    public static DartController Instance;

    [Header("配置")]
    public GameObject dartPrefab;       // 飞镖预制体
    public Transform throwPoint;        // 投掷点
    public float maxThrowForce = 20f;   // 最大投掷力
    public float maxAngle = 45f;        // 最大角度偏移

    [Header("状态")]
    public float currentPower = 5f;     // 当前力量值
    public float currentStability = 5f; // 当前稳定性值
    public float targetAngle = 0f;      // 目标角度
    public bool canThrow = true;        // 是否可以投掷

    private void Awake()
    {
        if (Instance == null)
            Instance = this;
        else
            Destroy(gameObject);
    }

    void Update()
    {
        if (GameManager_1.Instance.isPlaying && canThrow)
        {
            // 角度控制（左右箭头）
            if (Input.GetKey(KeyCode.LeftArrow))
                targetAngle = Mathf.Clamp(targetAngle - 1f, -maxAngle, maxAngle);
            if (Input.GetKey(KeyCode.RightArrow))
                targetAngle = Mathf.Clamp(targetAngle + 1f, -maxAngle, maxAngle);

            // 自动随机调整力量和稳定性（模拟波动）
            currentPower += Random.Range(-0.1f, 0.1f);
            currentPower = Mathf.Clamp(currentPower, -10f, 10f);
            
            currentStability += Random.Range(-0.1f, 0.1f);
            currentStability = Mathf.Clamp(currentStability, 0f, 10f);
        }
    }

    // 投掷飞镖
    public void ThrowDart()
    {
        if (!canThrow || !GameManager_1.Instance.isPlaying)
            return;

        canThrow = false;

        // 创建飞镖实例
        GameObject dart = Instantiate(dartPrefab, throwPoint.position, throwPoint.rotation);
        
        // 添加投掷力和角度偏移
        Rigidbody rb = dart.GetComponent<Rigidbody>();
        
        // 计算不稳定性导致的随机偏移（稳定性越低，偏移越大）
        float stabilityOffset = (10f - currentStability) * 0.1f;
        float randomAngleX = Random.Range(-stabilityOffset, stabilityOffset);
        float randomAngleY = Random.Range(-stabilityOffset, stabilityOffset);
        
        // 应用目标角度和随机偏移
        Quaternion throwRotation = Quaternion.Euler(randomAngleX, targetAngle + randomAngleY, 0f);
        Vector3 throwDirection = throwRotation * throwPoint.forward;
        
        // 应用投掷力（力量值影响投掷力度）
        float forceMultiplier = Mathf.InverseLerp(-10f, 10f, currentPower);
        rb.AddForce(throwDirection * forceMultiplier * maxThrowForce, ForceMode.Impulse);
        
        // 设置飞镖的分数检测
        DartScore dartScore = dart.GetComponent<DartScore>();
        if (dartScore != null)
        {
            dartScore.targetController = GameObject.FindObjectOfType<TargetController>();
            dartScore.onScoreCalculated += OnDartScored;
        }
        
        // 重置投掷状态（延迟恢复，模拟投掷动画时间）
        Invoke("ResetThrow", 1f);
    }

    // 重置投掷状态
    private void ResetThrow()
    {
        canThrow = true;
        
        // 随机化下一次投掷的初始力量和稳定性
        currentPower = Random.Range(3f, 8f);
        currentStability = Random.Range(3f, 8f);
    }

    // 飞镖计分回调
    private void OnDartScored(int score)
    {
        GameManager_1.Instance.AddScore(score);
    }
}